
import { http } from '@/utils/http'


/**
 * 报名列表
 * @returns ICaptcha 报名列表
 */
export const activitySignupGet = (activityId: String, crmid: String) => {
  return http.get('/campus/activity/signup', {
        "campus_activity_id": activityId,
        "crmid": crmid
      })
}

/**
 * 报名活动
 * @param 报名活动
 */
export const activitySignupPost = (activityId: Number) => {
  return http.post('/campus/activity/signup', {
    "campusActivityId": activityId,
    "checkedIn": true
  })
}

/**
 * 帖子评论列表
 * @param 帖子评论列表
 */
export const postDetailGet = (campusPostId: String) => {
  return http.get('/campus/comment', {
        "campusPostId": campusPostId,
        "pageSize": 22222
      })
}

/**
 * 创建帖子评论
 * @param 创建帖子评论
 */
export const postDetailPost = (postId: string, content: string) => {
  return http.post(`/campus/comment`, {
    "campusPostId": postId,
    "content": content
  })
}


/**
 * 接单列表 - 获取可以接单的活动列表
 * @param status 状态
 */
export const getAvailableOrders = (status: String) => {
  return http.get('/campus/order', {
    "status": status
  })
}

/**
 * 接单列表 - 获取可以接单的活动列表
 * @param user 用户
 * @param status 状态
 */
export const getAvailableOrdersUser = (user: String) => {
  return http.get('/campus/order', {
    "crmid": user
  })
}

function normalizeReward(value, { maxIntegerDigits = 13, scale = 2 } = {}) {
  if (value === null || value === undefined || value === '') {
    throw new Error('请输入奖励金额');
  }

  const num = Number(value);
  if (!Number.isFinite(num)) {
    throw new Error('奖励金额必须是数字');
  }

  // 限制小数位
  const fixed = Number(num.toFixed(scale));

  // 限制整数位
  const intDigits = Math.trunc(Math.abs(fixed)).toString().length;
  if (intDigits > maxIntegerDigits) {
    throw new Error(`奖励金额整数位不能超过 ${maxIntegerDigits} 位`);
  }

  return fixed;
}

/**
 * 创建跑腿订单
 * @param 创建跑腿订单
 */
export const availableOrdersPost = (type: String, reward: Number, address: String, status: String, contact: String, crman: String) => {
  return http.post('/campus/order', {
    "type": type,
    "reward": normalizeReward(reward, { maxIntegerDigits: 13, scale: 2 }),
    "status": status,
    "address": address,
    "contact": contact,
  })
}

/**
 * 跑腿订单接单
 * @param 跑腿订单接单
 */
export const availableOrdersPut = (type: String, id: String, status, price, address, contact, runner_id, reward) => {
  return http.put('/campus/order', {
    "type": type,
    "id": id,
    "status": status,
    "price": price,
    "address": address,
    "contact": contact,
    "runner_id": runner_id,
    "reward": reward
  })
}


/**
 * 聊天消息发送
 * @param 聊天消息发送
 */
export const chatMessagesPost = (user: String, content: String, conversationId: String) => {
  return http.post('/chat/messages', { 
    'inputs': {
      "web_token": ""
    },
    'user': user,
    'query': content,
    'conversationId': conversationId,
    'responseMode': 'blocking'
  })
}


/**
 * 获取聊天记录列表
 * @param 获取聊天记录列表
 */
export const chatConversationsPost = (user: String) => {
  return http.post('/chat/conversations', { 
    'user': user,
    'limit': 10
  })
}

/**
 * 获取聊天记录内容
 * @param 获取聊天记录内容
 */
export const messagesGet = (user: String, conversationId: String) => {
  return http.post('/chat/messages/get', { 
    'user': user,
    'conversationId': conversationId
  })
}